package study.transaction.seata.service;

import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import study.transaction.seata.client.AccountClient;
import study.transaction.seata.mapper.account.AccountBaseEO;
import study.transaction.seata.mapper.account.AccountBaseMapper;
import study.transaction.seata.mapper.user.UserBaseEO;
import study.transaction.seata.mapper.user.UserBaseMapper;

//https://www.jianshu.com/p/dfd5ae340011
@Slf4j
@Service
public class UserService {

    @Autowired
    private UserBaseMapper userBaseMapper;
    @Autowired
    private AccountBaseMapper accountBaseMapper;

    @Autowired
    private AccountClient accountClient;

    @GlobalTransactional
    public void checkIn(boolean throwEx) {
        //
        UserBaseEO ubEO = new UserBaseEO();
        ubEO.setUbLoginName("wzhj");
        ubEO.setUbLoginPwd("123123");
        userBaseMapper.insert(ubEO);
        if (throwEx) {
            throw new RuntimeException("我是一个业务异常");
        }

        //
        AccountBaseEO abEO = new AccountBaseEO();
        abEO.setAbUbId(ubEO.getUbId());
        accountBaseMapper.insert(abEO);
    }

    @GlobalTransactional
    public void checkIn() {
        //
        UserBaseEO ubEO = new UserBaseEO();
        ubEO.setUbLoginName("wzhj");
        ubEO.setUbLoginPwd("123123");
        userBaseMapper.insert(ubEO);

        //
        accountClient.checkIn(ubEO.getUbId(), true);
    }
}
